#pragma once
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

const std::string blank = " ";
const std::string sep   = "\n";

//变为-->len\n报文\n
std::string Encode(std::string & content)
{
    std::string package;
    package += std::to_string(content.size());
    package += sep;
    package += content;
    package += sep;

    return package;
}

//"len""\n""x op y""\n" 
bool Decode(std::string& package, std::string* content)
{
    size_t pos = package.find(sep);
    if(pos == std::string::npos)return false;
    
    //拿到长度 + 检验是否有一个完整报文
    std::string len_str = package.substr(0, pos);
    size_t len = std::stoi(len_str);
    ssize_t total_len = len_str.size() + len + 2;
    if(package.size() < total_len) return false;

    //提取有效载荷
    *content = package.substr(pos + 1, len);

    //移除这个一个报文: package.erase(0,total_len);
    package.erase(0,total_len);

    return true;
}


struct Request
{
public:
    int x;
    int y;
    char op;

public:
    Request(int data1, int data2, char _op)
        :x(data1)
        ,y(data2)
        ,op(_op)
    {}

    Request(){}

public:

    //构建"x + y"
    bool Serialize(std::string * out)
    {
#ifdef MySelf
        std::string s;
        s += std::to_string(x);
        s += blank;
        s += op;
        s += blank;
        s += std::to_string(y);
        *out = s;
        return true;
#else 
        Json::Value root;
        root["x"] = x;
        root["y"] = y;
        root["op"] = op;
        Json::FastWriter w;
        *out = w.write(root);
        return true;
#endif

    }
    
    //根据"x + y" --> 提取出 x op y
    bool Deserialize(std::string& in)
    {
#ifdef MySelf
        size_t left = in.find(blank);
        if(left == std::string::npos)return false;
        std::string part_x = in.substr(0, left);

        size_t right = in.rfind(blank);
        if(right == std::string::npos)return false;
        std::string part_y = in.substr(right + 1);

        if(left + 2 != right)return false;
        op = in[left + 1];

        x = std::stoi(part_x);
        y = std::stoi(part_y);

        return true;
#else
        Json::Value root;
        Json::Reader r;
        r.parse(in, root);

        x = root["x"].asInt();
        y = root["y"].asInt();
        op = root["op"].asInt();

        return true;
#endif
    }
};

struct Response
{
public: 
    int result;
    int code;           //状态码--o为可信, 非0不可信

public: 
    Response(int _result, int _code)
        :result(_result)
        ,code(_code)
    {}

    Response(){}

public:
    //构建"result code"
    bool Serialize(std::string * out)
    {
#ifdef MySelf

        std::string s;
        s += std::to_string(result);
        s += blank;
        s += std::to_string(code);

        *out = s;
        return true;
#else
        Json::Value root;
        Json::FastWriter w;
        root["result"] = result;
        root["code"] = code;
        *out = w.write(root);

        return true;
#endif
    }

    //根据"result code"提取result code
    bool Deserialize(std::string& in)
    {
#ifdef MySelf
        size_t pos = in.find(blank);
        if(pos == std::string::npos)return false;

        std::string part_left = in.substr(0, pos);
        std::string part_right = in.substr(pos + 1);

        result = std::stoi(part_left);
        code = std::stoi(part_right);
        return true;
#else

        Json::Value root;
        Json::Reader r;
        r.parse(in, root);
        result = root["result"].asInt();
        code = root["code"].asInt();

        return true;
#endif
    }
};









